Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
driver_MPL3115.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NXP Semiconductor
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*! \file driver_MPL3115.c
31  \brief Provides init() and read() functions for the MPL3115 pressure sensor/altimeter.
32 */
33 
34 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
35 #include "sensor_fusion.h" // Sensor fusion structures and types
36 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
37 #include "mpl3115_drv.h" // Low level IS-SDK prototype driver
38 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
39 #define MPL3115_MPERCOUNT 0.0000152587890625F // Convert 32-bit Altitude value to meters
40 #define MPL3115_CPERCOUNT 0.00390625F // Convert 16-bit Temperature value to Celcius
41 #define MPL3115_ACCEL_FIFO_SIZE 32
42 
43 #if F_USING_PRESSURE // driver is only pulled in if the appropriate flag is set in build.h
44 
45 // Command definition to read the WHO_AM_I value.
46 const registerreadlist_t MPL3115_WHO_AM_I_READ[] =
47 {
48  { .readFrom = MPL3115_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
49 };
50 
51 // Command definition to read the number of entries in the accel FIFO.
52 const registerreadlist_t MPL3115_F_STATUS_READ[] =
53 {
54  { .readFrom = MPL3115_STATUS, .numBytes = 1 }, __END_READ_DATA__
55 };
56 
57 // Command definition to read the number of entries in the accel FIFO.
58 registerreadlist_t MPL3115_DATA_READ[] =
59 {
60  { .readFrom = MPL3115_OUT_P_MSB, .numBytes = 5 }, __END_READ_DATA__
61 };
62 
63 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
64 const registerwritelist_t MPL3115_Initialization[] =
65 {
66  // write 0000 0000 = 0x00 to MPL3115_CTRL_REG1 to place the MPL3115 in Standby
67  // [7]: ALT=0
68  // [6]: RAW=0
69  // [5-3]: OS=000
70  // [2]: RST=0
71  // [1]: OST=0
72  // [0]: SBYB=0 to enter standby
73  { MPL3115_CTRL_REG1, 0x00, 0x00 },
74 
75  // write 1011 1001 = 0xB9 to configure MPL3115 and enter Active mode in Auto Acquisition mode and 1Hz ODR
76  // [7]: ALT=1 for altitude measurements
77  // [6]: RAW=0 to disable raw measurements
78  // [5-3]: OS=111 for OS ratio=128
79  // [2]: RST=0 do not enter reset
80  // [1]: OST=0 do not initiate a reading
81  // [0]: SBYB=1 to enter active mode
82  { MPL3115_CTRL_REG1, 0xB9, 0x00 },
83  __END_WRITE_DATA__
84 };
85 
86 // All sensor drivers and initialization functions have the same prototype.
87 // sfg is a pointer to the master "global" sensor fusion structure.
88 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
90 {
91  int32_t status;
92  uint8_t reg;
93  status = Register_I2C_Read(sensor->bus_driver, sensor->addr, MPL3115_WHO_AM_I, 1, &reg);
94  if (status==SENSOR_ERROR_NONE) {
95  sfg->Pressure.iWhoAmI = reg;
96  if (reg!=MPL3115_WHOAMI_VALUE) {
97  // return with error, whoAmI will retain default value of zero
98  return(SENSOR_ERROR_INIT);
99  }
100  } else {
101  // whoAmI will retain default value of zero
102  return(status);
103  }
104 
105  // Configure and start the MPL3115 sensor. This does multiple register writes
106  // (see MPL3115_Initialization definition above)
107  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, MPL3115_Initialization );
108 
109  // Stash some needed constants in the SF data structure for this sensor
110  sfg->Pressure.fmPerCount = MPL3115_MPERCOUNT; // Convert 32-bit Altitude value to meters
111  sfg->Pressure.fCPerCount = MPL3115_CPERCOUNT; // Convert 16-bit Temperature value to Celcius
112 
114  sfg->Pressure.isEnabled = true;
115 
116  return (status);
117 }
118 
119 int8_t MPL3115_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
120 {
121  uint8_t I2C_Buffer[6 * MPL3115_ACCEL_FIFO_SIZE]; // I2C read buffer
122  int8_t status; // I2C transaction status
123 
125  {
126  return SENSOR_ERROR_INIT;
127  }
128 
129  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, MPL3115_DATA_READ, I2C_Buffer );
130 
131  if (status==SENSOR_ERROR_NONE) {
132  // place the read buffer into the 32 bit altitude and 16 bit temperature
133  sfg->Pressure.iH = (I2C_Buffer[0] << 24) | (I2C_Buffer[1] << 16) | (I2C_Buffer[2] << 8);
134  sfg->Pressure.iT = (I2C_Buffer[3] << 8) | I2C_Buffer[4];
135 
136  // convert from counts to metres altitude and C
137  sfg->Pressure.fH = (float) sfg->Pressure.iH * sfg->Pressure.fmPerCount;
138  sfg->Pressure.fT = (float) sfg->Pressure.iT * sfg->Pressure.fCPerCount;
139  } else {
140  sfg->Pressure.fH = 0.0;
141  sfg->Pressure.fT = 0.0;
142  }
143 
144  return (status);
145 }
146 
147 
148 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
149 const registerwritelist_t MPL3115_IDLE[] =
150 {
151  // Set ACTIVE = other bits unchanged
152  { MPL3115_CTRL_REG1, 0x00, 0x01 },
153  __END_WRITE_DATA__
154 };
155 
156 // MPL3115_Idle places the sensor into Standby mode (wakeup time = 1 second)
157 int8_t MPL3115_Idle(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
158 {
159  int32_t status;
161  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, MPL3115_IDLE );
162  sensor->isInitialized = 0;
163  sfg->Pressure.isEnabled = false;
164  } else {
165  return SENSOR_ERROR_INIT;
166  }
167  return status;
168 }
169 #endif
int8_t MPL3115_Idle(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define MPL3115_MPERCOUNT
#define F_USING_PRESSURE
nominally 0x0008 if altimeter is to be used, 0x0000 otherwise
The top level fusion structure.
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
int8_t MPL3115_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
The sensor_fusion.h file implements the top level programming interface.
int8_t MPL3115_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
Provides function prototypes for driver level interfaces.
#define F_USING_TEMPERATURE
nominally 0x0010 if temp sensor is to be used, 0x0000 otherwise
#define MPL3115_CPERCOUNT
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
uint16_t addr
I2C address if applicable.
#define MPL3115_ACCEL_FIFO_SIZE